<?php

/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/**
 * Description of User
 *
 * @author chema_000
 */
class Admin_Models_tblUsers extends Libs_Model {
    public function __construct()
    {
        parent::__construct();
        $this->queryUnit = new Libs_QueryUnit();
    }

    private $id;
    private $email;
    private $password;
    private $phone;
    private $name;
    private $address;
    
    public function getId() {
        return $this->id;
    }

    public function getEmail() {
        return $this->email;
    }

    public function getPassword() {
        return $this->password;
    }

    public function getPhone() {
        return $this->phone;
    }

    public function getName() {
        return $this->name;
    }

    public function getAddress() {
        return $this->address;
    }

    public function setId($id) {
        $this->id = $id;
    }

    public function setEmail($email) {
        $this->email = $email;
    }

    public function setPassword($password) {
        $this->password = $password;
    }

    public function setPhone($phone) {
        $this->phone = $phone;
    }

    public function setName($name) {
        $this->name = $name;
    }

    public function setAddress($address) {
        $this->address = $address;
    }

    
    private function setUserValues($row)
    {
        $admin = new Admin_Models_tblUsers();
        $admin->setId($row['id']);
        $admin->setEmail($row['email']);
        $admin->setPassword($row['password']);
        $admin->setName($row['name']);
        $admin->setPhone($row['phone']);
        $admin->setAddress($row['address']);
        return $admin;
    }

    private function getColumnAndValue(Admin_Models_tblUsers $user, $isKey = false)
    {
        $arr_data= array();
        if ($isKey) {
            $arr_data['id']=$user->getId();
        }
        $arr_data['email']=$user->getEmail();
        $arr_data['password']=$user->getPassword();
        $arr_data['name']=$user->getName();
        $arr_data['phone']=$user->getPhone();
        $arr_data['phone']=$user->getAddress();
        return $arr_data;
    }

    public function checkLogin($email, $password)
    {
        $admin = null;
        if (!empty($email) && !empty($password)) {
            $execute = $this->queryUnit->getSelect("tbl_users", "email='$email' and password='$password'");
            if (mysql_num_rows($execute) > 0) {
                while ($row = mysql_fetch_assoc($execute)) {
                    $admin = $this->setUserValues($row);
                }
            }
        }
        return $admin;
    }

    public function getSearchUsers($keyword)
    {
        $listAdmin = array();
        $execute = $this->queryUnit->getSelect('tbl_users',"email LIKE '$$keyword$' OR name LIKE '%$keyword%' ");
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $admin = $this->setUserValues($row);
                $listAdmin[] = $admin;
            }
        }
        return $listAdmin;
    }

    public function getAllUsers()
    {
        $listAdmin = array();
        $execute = $this->queryUnit->getSelect('tbl_users');
        if (mysql_num_rows($execute) > 0) {
            while ($row = mysql_fetch_assoc($execute)) {
                $admin = $this->setUserValues($row);
                $listAdmin[] = $admin;
            }
        }
        return $listAdmin;
    }

    public function getUserByID($id)
    {
        $admin = null;
        //if (!empty($email) && !empty($password)) {
            $execute = $this->queryUnit->getSelect("tbl_users", "id='$id'");
            if (mysql_num_rows($execute) > 0) {
                while ($row = mysql_fetch_assoc($execute)) {
                    $admin = $this->setUserValues($row);
                }
            }
        //}
        return $admin;
    }
    public function getUserByEmail($email)
    {
        $admin = null;
            $execute = $this->queryUnit->getSelect("tbl_users", "email='$email'");
            if (mysql_num_rows($execute) > 0) {
                while ($row = mysql_fetch_assoc($execute)) {
                    $admin = $this->setUserValues($row);
                }
            }
        return $admin;
    }

    public function insertUser(Admin_Models_tblUsers $user)
    {
        $execute=$this->queryUnit->getInsert('tbl_users', $this->getColumnAndValue($user));
        if($execute){
            return true;
        }
        return false;
    }

    public function updateUser(Admin_Models_tblUsers $user, $id)
    {
        $execute=$this->queryUnit->getUpdate('tbl_users', $this->getColumnAndValue($user),"id='$id'");
        if($execute){
            return true;
        }
        return false;
    }

    public function deleteUser($id)
    {
        $execute= $this->queryUnit->getDelete('tbl_users',"id='$id'");
        if($execute){
            return true;
        }
        return false;
    }

    public function isAdmin(){
        $id = $_SESSION['id'];

        $roleUserModel = new Admin_Models_tblRoleUser();
        $rolesUserID = $roleUserModel->getAllRoleByID($id);

        foreach($rolesUserID as $key =>$roleUser){
            if($roleUser->getRoleId() == 1){
                return true;
            }else{
                return false;
            }
        }  
    }

    public function isEditor(){
        $id = $_SESSION['id'];

        $roleUserModel = new Admin_Models_tblRoleUser();
        $rolesUserID = $roleUserModel->getAllRoleByID($id);

        foreach($rolesUserID as $key =>$roleUser){
            if($roleUser->getRoleId() === 2){
                return true;
            }else{
                return false;
            }
        }  
    }
}
